package com.pan.admin.module.loanProduct.service.impl;

import com.pan.admin.common.annotation.DataScope;
import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.module.api.dao.ApiKeyDao;
import com.pan.admin.module.channel.dao.LoanChannelDao;
import com.pan.admin.module.loanProduct.dao.LoanProductDao;
import com.pan.admin.module.loanProduct.dao.LoanProductPlanDao;
import com.pan.admin.module.loanProduct.dao.LoanProductRechargeDao;
import com.pan.admin.module.loanProduct.entity.po.LoanProductRechargePO;
import com.pan.admin.module.loanProduct.entity.qo.LoanProductRechargeQO;
import com.pan.admin.module.loanProduct.service.LoanProductRechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 机构充值记录
 *
 * @author caoxin
 */
@Service
public class LoanProductRechargeServiceImpl implements LoanProductRechargeService {

    @Autowired
    private LoanProductRechargeDao loanProductRechargeDao;
    @Autowired
    private ApiKeyDao apiKeyDao;
    @Autowired
    private LoanProductDao loanProductDao;
    @Autowired
    private LoanProductPlanDao loanProductPlanDao;
    @Autowired
    private LoanChannelDao loanChannelDao;

    /**
     * 查询机构充值记录列表(有数据权限)
     *
     * @param loanProductRechargeQO 机构充值记录
     * @return 机构充值记录
     */
    @Override
    @Transactional(readOnly = true)
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<LoanProductRechargePO> listWithDataScope(LoanProductRechargeQO loanProductRechargeQO) {
        List<LoanProductRechargePO> list = this.loanProductRechargeDao.listWithDataScope(loanProductRechargeQO);
        list.forEach(item -> {
            Long loanChannelId = item.getLoanChannelId();
            if (loanChannelId == null) {
                return;
            }
            String loanChannelName = this.loanChannelDao.findLoanChannelNameByLoanChannelId(loanChannelId);
            item.setLoanChannelName(loanChannelName);
        });
        list.forEach(item -> {
            Long loanProductId = item.getLoanProductId();
            if (loanProductId == null) {
                return;
            }
            String LoanProductName = this.loanProductDao.findLoanProductNameByLoanProductId(loanProductId);
            item.setLoanProductName(LoanProductName);
        });
        list.forEach(item -> {
            Long loanProductPlanId = item.getLoanProductPlanId();
            if (loanProductPlanId == null) {
                return;
            }
            String loanProductPlanName = this.loanProductPlanDao.findLoanProductPlanNameByLoanProductPlanId(loanProductPlanId);
            item.setLoanProductPlanName(loanProductPlanName);
        });
        return list;
    }

    /**
     * 新增机构充值记录
     *
     * @param loanProductRecharge 机构充值记录
     * @return 结果
     */
    @Override
    public int insertLoanProductRecharge(LoanProductRechargePO loanProductRecharge) {
        loanProductRecharge.setCreateTime(DateUtils.getNowDate());
        return this.loanProductRechargeDao.insertLoanProductRecharge(loanProductRecharge);
    }

}
